﻿#define PP_2010
//#define PP_2010

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using Microsoft.Kinect;

using Office = Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;

namespace KinectPowerPointControl_sample1_Fukidashi
{
    class PowerPointController
    {
        // ファイル利用
        private string stCurrentDir;

        // PowerPoint連携用
        private PowerPoint.Application objPPT;
        private PowerPoint.Shape fukidashi;

        // 音声再生用
        private System.Media.SoundPlayer applausePlayer;
        private System.Media.SoundPlayer gongPlayer;

        // 追加したシェープへのポインタ
        private ArrayList addShapes;

        // 追加したシェープへのポインタ
        private ArrayList posShapes;

        // 起動時のエフェクトのリスト
        private ArrayList originalEffectList;

        private PowerPoint.PpEntryEffect slideEffect;
        private PowerPoint.PpSoundEffectType soundEffectType;
        private string soundEffectFile;

        public    PowerPointController()
        {
            this.objPPT = new PowerPoint.Application();                
            this.stCurrentDir = System.Environment.CurrentDirectory ;

            this.applausePlayer = new System.Media.SoundPlayer(this.stCurrentDir + "\\..\\..\\sound\\applause.wav");
            this.gongPlayer = new System.Media.SoundPlayer(this.stCurrentDir + "\\..\\..\\sound\\gong.wav");
            this.addShapes = new ArrayList();
            this.posShapes = new ArrayList();
            this.originalEffectList = new ArrayList();

            // 起動時のエフェクトのリストを保存しておく
            try
            {
                for (int i = 0; i < this.objPPT.ActivePresentation.Slides.Count; i++)
                {
                    //System.Diagnostics.Debug.WriteLine(  i + " = " +this.objPPT.ActivePresentation.Slides[i + 1].SlideShowTransition.EntryEffect);
                    originalEffectList.Add(this.objPPT.ActivePresentation.Slides[i + 1].SlideShowTransition.EntryEffect);
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        // 終了時の処理
        public void exit()
        {
            // 追加したシェープを削除する
            foreach (PowerPoint.Shape s in addShapes)
            {
                s.Delete();
            }
            foreach (PowerPoint.Shape s in posShapes)
            {
                s.Delete();
            }
            
            //起動時のエフェクトに戻す
            for (int i = 0; i < this.objPPT.ActivePresentation.Slides.Count; i++)
            {
                //PowerPoint.ppEntryEffect
                this.objPPT.ActivePresentation.Slides[i + 1].SlideShowTransition.EntryEffect = (PowerPoint.PpEntryEffect)this.originalEffectList[i];
            }
        }

        public void setEffect(HandHeight height)
        {
            soundEffectFile = "";

            if (height == HandHeight.High)
            {
                //slideEffect = PowerPoint.PpEntryEffect.ppEffectUncoverUp;
                slideEffect = PowerPoint.PpEntryEffect.ppEffectNewsflash;
                soundEffectType = PowerPoint.PpSoundEffectType.ppSoundFile;
                soundEffectFile = this.stCurrentDir + "\\..\\..\\sound\\explode.wav";
            }
            else if (height == HandHeight.Normal)
            {
                slideEffect = PowerPoint.PpEntryEffect.ppEffectNone;
            }
            else if (height == HandHeight.Low)
            {
                slideEffect = PowerPoint.PpEntryEffect.ppEffectUncoverDown;
            }
            else if (height == HandHeight.Lowest)
            {
                slideEffect = PowerPoint.PpEntryEffect.ppEffectBlindsHorizontal;
            }
        }

        // スライドを１つ進める
        public void next(){
            try
            {
                int currentSlideNum = this.objPPT.ActivePresentation.SlideShowWindow.View.Slide.SlideIndex;
                if (currentSlideNum < this.objPPT.ActivePresentation.Slides.Count)
                {
                    this.objPPT.ActivePresentation.Slides[currentSlideNum + 1].SlideShowTransition.EntryEffect
                        = slideEffect;

                    if (soundEffectFile.Length > 0)
                    {
                        this.objPPT.ActivePresentation.Slides[currentSlideNum + 1].SlideShowTransition.
                            SoundEffect.ImportFromFile(soundEffectFile);
                    }
                    else
                    {
                        this.objPPT.ActivePresentation.Slides[currentSlideNum + 1].SlideShowTransition.
                            SoundEffect.Type = PowerPoint.PpSoundEffectType.ppSoundNone;
                    }

                    this.objPPT.ActivePresentation.SlideShowWindow.View.Next();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        // スライドを１つ戻す
        public void back()
        {
            try
            {
                // PowerPoint連携用
                int currentSlideNum = objPPT.ActivePresentation.SlideShowWindow.View.Slide.SlideIndex;
                if (currentSlideNum > 1)
                {
                    objPPT.ActivePresentation.Slides[currentSlideNum - 1].SlideShowTransition.EntryEffect
                            = PowerPoint.PpEntryEffect.ppEffectUncoverDown;
                    objPPT.ActivePresentation.SlideShowWindow.View.Previous();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        // 猫を表示させる
        public void showCat( int catNo)
        {
            try
            {
                int currentSlideNum = objPPT.ActivePresentation.SlideShowWindow.View.Slide.SlideIndex;
                PowerPoint.Slide slide = objPPT.ActivePresentation.Slides[currentSlideNum];

                PowerPoint.Shape catPic = slide.Shapes.AddPicture(this.stCurrentDir + "\\..\\..\\img\\cat" + catNo + ".jpg", Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue
                                                                    , 100, 80);

                addShapes.Add( catPic);
        
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        // スケルトン描画
        public void drawSkeleton( Hashtable positions)
        {
            try
            {
                int currentSlideNum = objPPT.ActivePresentation.SlideShowWindow.View.Slide.SlideIndex;
                PowerPoint.Slide slide = objPPT.ActivePresentation.Slides[currentSlideNum];

                foreach (PowerPoint.Shape c in posShapes)
                {
                    if (c != null)
                    {
                        c.Delete();
                    }
                }
                posShapes.Clear();


                ColorImagePoint head = (ColorImagePoint)positions["Head"];
                ColorImagePoint hand_right = (ColorImagePoint)positions["HandRight"];
                ColorImagePoint hand_left = (ColorImagePoint)positions["HandLeft"];
                ColorImagePoint shoulder_right = (ColorImagePoint)positions["ShoulderRight"];
                ColorImagePoint shoulder_left = (ColorImagePoint)positions["ShoulderLeft"];
                ColorImagePoint shoulder_center = (ColorImagePoint)positions["ShoulderCenter"];

                ColorImagePoint knee_right = (ColorImagePoint)positions["KneeRight"];
                ColorImagePoint knee_left = (ColorImagePoint)positions["KneeLeft"];
                ColorImagePoint hip_center = (ColorImagePoint)positions["HipCenter"];

                //PowerPoint.Shape posLine = slide.Shapes.AddLine( head.X, head.Y, , 300);

                PowerPoint.Shape cHead = slide.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeOval, head.X, head.Y, 50, 50);
                PowerPoint.Shape cHandRight = slide.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeOval, hand_right.X, hand_right.Y, 20, 20);
                PowerPoint.Shape cHandLeft = slide.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeOval, hand_left.X, hand_left.Y, 20, 20);

                PowerPoint.Shape cShoulderRight = slide.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeOval, shoulder_right.X, shoulder_right.Y, 20, 20);
                PowerPoint.Shape cShoulderLeft = slide.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeOval, shoulder_left.X, shoulder_left.Y, 20, 20);
                PowerPoint.Shape cShoulderCenter = slide.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeOval, shoulder_center.X, shoulder_center.Y, 30, 30);

                PowerPoint.Shape cHipCenter = slide.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeOval, hip_center.X, hip_center.Y, 30, 30);
                PowerPoint.Shape cKneeRight = slide.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeOval, knee_right.X, knee_right.Y, 20, 20);
                PowerPoint.Shape cKneeLeft = slide.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeOval, knee_left.X, knee_left.Y, 20, 20);


                posShapes.Add(cHead);
                posShapes.Add(cHandLeft);
                posShapes.Add(cHandRight);
                posShapes.Add(cShoulderCenter);
                posShapes.Add(cShoulderLeft);
                posShapes.Add(cShoulderRight);

                posShapes.Add(cHipCenter);
                posShapes.Add(cKneeLeft);
                posShapes.Add(cKneeRight);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }



        // PowerPoint連携用
        // 現在表示中のスライド（スライドショー中でなとも可）に、オートシェイプで吹き出しを追加する。
        private void showFukidashi(String textToShow, float top, float left)
        {
            try
            {
                PowerPoint.Slide slide =
                    objPPT.ActivePresentation.Slides[1];

                if (fukidashi != null) { fukidashi.Delete(); } //表示済みのテキストボックスを削除 

                fukidashi = slide.Shapes.AddShape(
                Office.MsoAutoShapeType.msoShapeCloudCallout,
                top, left, 230, 100);

                fukidashi.TextFrame.TextRange.Text = textToShow;
                fukidashi.TextFrame.TextRange.Font.Size = 32;
                fukidashi.Line.ForeColor.RGB = 0x000000;
                fukidashi.Fill.BackColor.RGB = 0xFFFFFF;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //拍手の効果音を鳴らす
        internal void playApplauseSound()
        {
            try
            {
                this.applausePlayer.Play();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //電灯のスイッチをオンオフする
        private PowerPoint.Shape blackCeiling;
        internal void turnBlack()
        {
            try
            {
                int currentSlideNum = objPPT.ActivePresentation.SlideShowWindow.View.Slide.SlideIndex;
                PowerPoint.Slide slide = this.objPPT.ActivePresentation.Slides[currentSlideNum];

                if (blackCeiling != null && blackCeiling.Visible == Office.MsoTriState.msoTrue)
                {
                    blackCeiling.Delete();  //表示済みのテキストボックスを削除
                    blackCeiling = null;
                }
                else
                {

                    blackCeiling = slide.Shapes.AddShape(
                    Office.MsoAutoShapeType.msoShapeRectangle,
                    0, 0, 0, 0);

                    blackCeiling.Line.ForeColor.RGB = 0x000000;
                    blackCeiling.Fill.ForeColor.RGB = 0x000000;
                    blackCeiling.Height = 1000;
                    blackCeiling.Width = 2000;

                    addShapes.Add(blackCeiling);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //ゴング三回（プレゼン終了）の効果音を鳴らす
        internal void playGong()
        {
            try
            {
                this.gongPlayer.Play();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        // フォントサイズを大きくする
        public void changeFontSize( int changeSize)
        {
            try
            {
                int currentSlideNum = objPPT.ActivePresentation.SlideShowWindow.View.Slide.SlideIndex;
                PowerPoint.Slide slide = objPPT.ActivePresentation.Slides[currentSlideNum];

                foreach (PowerPoint.Shape shape in slide.Shapes)
                {
                    if (shape.HasTextFrame == Office.MsoTriState.msoTrue)
                    {
                        // 自動調整をオフにする
                        shape.TextFrame.AutoSize = PowerPoint.PpAutoSize.ppAutoSizeNone;

                        shape.TextFrame.TextRange.Font.Size += changeSize;
                    }
                }

                //PowerPoint.Shape catPic = slide.Shapes.AddPicture(this.stCurrentDir + "\\..\\..\\img\\cat1.jpg", Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue
                //                                                    , 100, 80);

                //addShapes.Add(catPic);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //巨大な×マークを出力する
        internal void drawBigX()
        {
            try
            {
                int currentSlideNum = objPPT.ActivePresentation.SlideShowWindow.View.Slide.SlideIndex;
                PowerPoint.Slide slide = this.objPPT.ActivePresentation.Slides[currentSlideNum];

                PowerPoint.Shape bigX = slide.Shapes.AddShape(
                Office.MsoAutoShapeType.msoShapeCross,
                150, 100, 0, 0);

                bigX.Line.ForeColor.RGB = 0x0000FF;
                bigX.Fill.ForeColor.RGB = 0x0000FF;
                bigX.Height = 400;
                bigX.Width = 400;
                bigX.Rotation = 45;
                bigX.Adjustments[1] = (float)0.37;

                addShapes.Add(bigX);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //ミドルキック判定時
        internal void middleKick()
        {
            throw new NotImplementedException();
        }
    }
}
